package com.hdcx.Minfresh.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.AddressActivity;
import com.hdcx.Minfresh.activity.CollectActivity;
import com.hdcx.Minfresh.activity.HistoryActivity;
import com.hdcx.Minfresh.activity.MainActivity;
import com.hdcx.Minfresh.activity.MineFeedbackActivity;
import com.hdcx.Minfresh.activity.MineMsgActivity;
import com.hdcx.Minfresh.activity.MineMyPointActivity;
import com.hdcx.Minfresh.activity.MineOrderListActivity;
import com.hdcx.Minfresh.activity.MineUserInfoActivity;
import com.hdcx.Minfresh.activity.MyMoneyActivity;
import com.hdcx.Minfresh.activity.PointChangeReActivity;
import com.hdcx.Minfresh.activity.WebViewActivity;
import com.hdcx.Minfresh.bean.CartWrapper;
import com.hdcx.Minfresh.bean.MineIndexWrapper;
import com.hdcx.Minfresh.bean.MineUserInfo;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.manager.CacheManager;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/9.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {
    DisplayImageOptions options;
    TextView tv_name,tv_phone,tv_phone2;
    ImageView img_main;
    String VadeoUrl;
    private TextView mTextViewCache;

    @Override
    public int getLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView(View view) {
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tv_phone = (TextView) view.findViewById(R.id.tv_phone);
        tv_phone2 = (TextView) view.findViewById(R.id.tv_phone2);
        img_main = (ImageView) view.findViewById(R.id.img_main);
        mTextViewCache = (TextView) view.findViewById(R.id.textViewCache);
        String totalCacheSize;
        totalCacheSize = "0kb";
        try {
            totalCacheSize = CacheManager.getTotalCacheSize(getActivity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mTextViewCache.setText(totalCacheSize);
        view.findViewById(R.id.layout_mypoint).setOnClickListener(this);
        view.findViewById(R.id.layout_msg).setOnClickListener(this);
        view.findViewById(R.id.layout_feedback).setOnClickListener(this);
        view.findViewById(R.id.layout_orderlist).setOnClickListener(this);
        view.findViewById(R.id.layout_userinfo).setOnClickListener(this);
        view.findViewById(R.id.layout_address).setOnClickListener(this);
        view.findViewById(R.id.layout_collect).setOnClickListener(this);
        view.findViewById(R.id.bt_out).setOnClickListener(this);
        view.findViewById(R.id.layout_mymoney).setOnClickListener(this);
        view.findViewById(R.id.layout_clean).setOnClickListener(this);
        view.findViewById(R.id.layout_about).setOnClickListener(this);
        view.findViewById(R.id.layout_help).setOnClickListener(this);
        view.findViewById(R.id.layout_quan).setOnClickListener(this);
        view.findViewById(R.id.layout_fuwu).setOnClickListener(this);
        view.findViewById(R.id.layout_dfk).setOnClickListener(this);
        view.findViewById(R.id.layout_dps).setOnClickListener(this);
        view.findViewById(R.id.layout_psz).setOnClickListener(this);
        view.findViewById(R.id.layout_ydd).setOnClickListener(this);
        view.findViewById(R.id.layout_pointchange).setOnClickListener(this);
        view.findViewById(R.id.layout_history).setOnClickListener(this);
        view.findViewById(R.id.layout_zhifubao).setOnClickListener(this);
        view.findViewById(R.id.bt_out).setOnClickListener(this);

        TextView app_version = (TextView) view.findViewById(R.id.app_version);
        app_version.setText( "闽鲜购v"+ AppUtil.getAppVersion() ) ;


        //view.findViewById(R.id.app_version).setText( "闽鲜购"+ AppUtil.getAppVersion());
    }

    @Override
    public void initData() {
        post_main();
        post_data();
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("MineFragment", "MineFragment--onStart--");
        post_main();
    }

    private void post_data() {
        OkHttpUtils.postString()
                .url(MyUrl.USERINDEX)
                .content("")
                .build()
                .execute(new Callback<MineIndexWrapper>() {
                    @Override
                    public MineIndexWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, MineIndexWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(MineIndexWrapper response) {
                        if (response.getStatus() == 1) {
                            tv_phone2.setText(response.getTel());
                            VadeoUrl = response.getVideo();
                        }
                    }
                });
    }


    private void post_main() {
        Log.e("MineFragment", "MineFragment--post_main--");
        UserInfoBean user =  ShpfUtil.getObject(ShpfUtil.LOGIN);
        JSONObject json = new JSONObject();
        try {
            json.put("uid",user.getUserinfo().getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.USERINFOGET)
                .content(json.toString())
                .build()
                .execute(new Callback<MineUserInfo>() {
                    @Override
                    public MineUserInfo parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, MineUserInfo.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(MineUserInfo response) {
                        if (response.getStatus() == 1) {
                            setValue(response);
                        }
                    }
                });
    }

    private void setValue(MineUserInfo response) {
        tv_phone.setText(response.getUser().getMobile());
        tv_name.setText(response.getUser().getNick_name());
        ImageLoader imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder().cacheOnDisc(true).showImageOnLoading(R.drawable.img01)
                .showImageForEmptyUri(R.drawable.img01)
                .imageScaleType(ImageScaleType.EXACTLY)
                .showImageOnFail(R.drawable.img01).imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new RoundedBitmapDisplayer(AppUtil.dipToPixels(40), 0))
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.displayImage(response.getUser().getFace(), img_main, options);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(AppUtil.isFrash){
            post_main();
            AppUtil.isFrash = false;
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.layout_mypoint:
                intent = new Intent(getActivity(), MineMyPointActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_msg:
                intent = new Intent(getActivity(), MineMsgActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_feedback:
                intent = new Intent(getActivity(), MineFeedbackActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_orderlist:
                intent = new Intent(getActivity(), MineOrderListActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_dfk:
                intent = new Intent(getActivity(), MineOrderListActivity.class);
                intent.putExtra("type",1);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_dps:
                intent = new Intent(getActivity(), MineOrderListActivity.class);
                intent.putExtra("type",10);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_psz:
                intent = new Intent(getActivity(), MineOrderListActivity.class);
                intent.putExtra("type",20);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_ydd:
                intent = new Intent(getActivity(), MineOrderListActivity.class);
                intent.putExtra("type",30);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_userinfo:
                intent = new Intent(getActivity(), MineUserInfoActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_address:
                intent = new Intent(getActivity(), AddressActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_collect:
                intent = new Intent(getActivity(), CollectActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.bt_out:
                ShpfUtil.remove(ShpfUtil.LOGIN);
                MainActivity activity = (MainActivity) getActivity();
                activity.changeFragment(activity.tv_ta01);
                break;
            case R.id.layout_mymoney:
                intent = new Intent(getActivity(), MyMoneyActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_clean:
//                intent = new Intent(getActivity(), CleanCacheActivity.class);
//                getActivity().startActivity(intent);
//                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                final android.app.AlertDialog.Builder alterDialog = new android.app.AlertDialog.Builder(getActivity());
                alterDialog.setMessage("是否清空缓存？");
                alterDialog.setCancelable(true);
                alterDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserInfoBean loginWrapper = ShpfUtil.getObject(ShpfUtil.LOGIN);
                        CartWrapper cart = ShpfUtil.getObject(AppUtil.getUserId());
                        CacheManager.clearAllCache(getActivity());
                        ShpfUtil.setObject(ShpfUtil.LOGIN, loginWrapper);
                        if (cart != null) {
                            ShpfUtil.setObject(AppUtil.getUserId(), cart);
                        }
                        mTextViewCache.setText("0kb ");
                        Toast.makeText(getActivity(), "成功清除缓存！", Toast.LENGTH_SHORT).show();
                    }
                });
                alterDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                alterDialog.show();
                break;
            case R.id.layout_pointchange:
                intent = new Intent(getActivity(), PointChangeReActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_history:
                intent = new Intent(getActivity(), HistoryActivity.class);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_about:
                intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", MyUrl.URL+"/api/index/about.html");
                intent.putExtra("title", "关于我们");
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_help:
                intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", MyUrl.URL+"/api/user/help.html");
                intent.putExtra("title", "帮助中心");
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_quan:
                UserInfoBean loginData = ShpfUtil.getObject(ShpfUtil.LOGIN);
                intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",MyUrl.URL+"/api/user/getdiscount.html?uid=" + loginData.getUserinfo().getId());
                intent.putExtra("title", "优惠券");
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_fuwu:
                intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", MyUrl.URL+"/api/index/server.html");
                intent.putExtra("title", "服务条款");
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
            case R.id.layout_zhifubao:
                intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",VadeoUrl);
                intent.putExtra("title", "在线直播");
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;
        }
    }
}
